<!--
* description: 订货参数维护-编辑
-->
<template>
  <section class="editCarBrand">
    <el-dialog
      v-dialogDrag
      :close-on-click-modal="false"
      title="编辑订货参数"
      :append-to-body="true"
      :visible.sync="curPopupsVisible"
      @close="sendCode"
      width="1100px"
    >
      <dialogHeader slot="title" title="编辑订货参数" :styleType="$store.state.app.dialogType"></dialogHeader>
      <div class="filter-container filter-params" style="padding-bottom: 10px;">
        <el-row :gutter="26">
          <component
            v-for="comp in tableComponents.filter(o => o.isMust === true && o.topComponents===true)"
            :ref="comp.isRequire ? comp.isRequire+ comp.compKey : comp.compKey"
            :validrule="comp.validrule"
            :key="comp.compKey"
            :codeField="comp.codeField"
            :textField="comp.textField"
            :popupsKey="comp.compKey"
            :is="comp.component"
            :code="formField[comp.codeField]"
            :text="formField[comp.textField]"
            @changeCode="getComponentCode"
            @focusCode="getFocusCode"
            :disabled="comp.disabled"
            :isRequire="comp.isRequire"
            :span="comp.span || 8"
            :isMul="comp.isMul"
            :labelName="comp.labelName"
            :lookuptype="comp.lookuptype"
            :options="comp.options"
            :dateOptionsType="comp.dateOptionsType"
            :readonly="comp.readonly"
            :showPlaceholder="comp.showPlaceholder"
            :isInput="comp.isInput"
            :dataToObject="comp.dataToObject"
          ></component>
        </el-row>
        <el-divider content-position="left">订货计算参数</el-divider>
        <el-row :gutter="26">
          <component
            v-for="comp in tableComponents.filter(o => o.isMust === true && o.bottomComponents===true)"
            :ref="comp.isRequire ? comp.isRequire+ comp.compKey : comp.compKey"
            :validrule="comp.validrule"
            :key="comp.compKey"
            :codeField="comp.codeField"
            :textField="comp.textField"
            :popupsKey="comp.compKey"
            :is="comp.component"
            :code="formField[comp.codeField]"
            :text="formField[comp.textField]"
            @changeCode="getComponentCode"
            @focusCode="getFocusCode"
            :disabled="comp.disabled"
            :isRequire="comp.isRequire"
            :span="comp.span || 8"
            :isMul="comp.isMul"
            :labelName="comp.labelName"
            :lookuptype="comp.lookuptype"
            :options="comp.options"
            :dateOptionsType="comp.dateOptionsType"
            :readonly="comp.readonly"
            :showPlaceholder="comp.showPlaceholder"
            :isInput="comp.isInput"
            :dataToObject="comp.dataToObject"
            :labelWidth="comp.labelWidth"
            :isSlot="comp.isSlot"
            :slotName="comp.codeField"
            :slotWidth="comp.slotWidth"
            :o-fields="comp.oFields"
          >
            <template slot="SAFETY_FACTOR">
              <div class="slotDom">
                <span class="safety_factorSpan">默认为1</span>
              </div>
            </template>
            <template slot="ORDER_PERIOD">
              <div class="slotDom">
                <span class="safety_factorSpan">（个月）</span>
              </div>
            </template>
            <template slot="ARRIVAL_PERIOD">
              <div class="slotDom">
                <span class="safety_factorSpan">（天）</span>
              </div>
            </template>
            <template slot="MONTH_QTY">
              <div class="slotDom">
                <el-checkbox
                  v-model="formField.AUTO_MONTH_QTY"
                  false-label="0"
                  true-label="1"
                  @change="val=>{changeCheckBox(val, 'AUTO_MONTH_QTY')}"
                >Auto</el-checkbox>
              </div>
            </template>
            <template slot="SALE_FLUCTATE_DIFF">
              <div class="slotDom">
                <el-checkbox
                  v-model="formField.AUTO_SFD"
                  false-label="0"
                  true-label="1"
                  @change="val=>{changeCheckBox(val, 'AUTO_SFD')}"
                >Auto</el-checkbox>
              </div>
            </template>
            <template slot="TARGET_SERVICE_RATE">
              <div class="slotDom" style="width:130px;">
                <span class="safety_factorSpan">
                  默认95%
                  <el-link type="primary" title="对照参数" @click="isShow = true">对照参数</el-link>
                </span>
              </div>
            </template>
            <template slot="SAFETY_STORE">
              <div class="slotDom">
                <el-checkbox
                  v-model="formField.AUTO_SS"
                  false-label="0"
                  true-label="1"
                  @change="val=>{changeCheckBox(val, 'AUTO_SS')}"
                >Auto</el-checkbox>
              </div>
            </template>
            <template slot="AMC">
              <div class="slotDom">
                <el-checkbox
                  v-model="formField.AUTO_AMC"
                  false-label="0"
                  true-label="1"
                  @change="val=>{changeCheckBox(val, 'AUTO_AMC')}"
                >Auto</el-checkbox>
              </div>
            </template>
            <template slot="ARRIVE_FLUCTATE_DIFF">
              <div class="slotDom">
                <el-checkbox
                  v-model="formField.AUTO_AFD"
                  false-label="0"
                  true-label="1"
                  @change="val=>{changeCheckBox(val, 'AUTO_AFD')}"
                >Auto</el-checkbox>
              </div>
            </template>
          </component>
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons"
          :key="comp.compKey"
          :type="comp.type"
          @click="comp.clickEvent"
        >{{comp.text}}</el-button>
      </div>
    </el-dialog>
    <detail v-if="isShow" @detailClose="detailClose" :isShow="isShow" />
  </section>
</template>
<script>
import { formMixins } from "@/components/wms/mixins/formMixins";
import request from "@/utils/request";
import { wmsApis } from "@/api/wms/allocation/api";
import storage from "@/utils/storage";
import { log } from "util";
export default {
  // 组件混入对象
  mixins: [formMixins],
  components: {
    dialogHeader: () => import("@/components/dialogHeader"),
    detail: () => import("./detail")
  },
  data() {
    return {
      // 保存API配置对象
      apiConfig: wmsApis.savePartList,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.save(),
          text: "保存" //"保存"
        },
        {
          compKey: "btnKey2",
          type: "",
          size: "small",
          clickEvent: () => this.reset(),
          text: "重置" //"重置"
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "edit_compKey1",
          labelName: "备件编码",
          codeField: "PART_NO",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true,
          readonly: true,
          showPlaceholder: "",
          topComponents: true
        },
        {
          compKey: "edit_compKey2",
          labelName: "备件名称",
          codeField: "PART_NAME",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true,
          readonly: true,
          showPlaceholder: "",
          topComponents: true
        },
        {
          compKey: "edit_compKey3",
          labelName: "供应商编号",
          codeField: "SUPPLIER_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true,
          readonly: true,
          showPlaceholder: "",
          topComponents: true
        },
        {
          compKey: "edit_compKey4",
          labelName: "仓库名称",
          codeField: "WAREHOUSE_NAME",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true,
          readonly: true,
          showPlaceholder: "",
          topComponents: true
        },
        //   {
        //   compKey: "edit_compKey6",
        //   labelName: "仓库ID",
        //   codeField: "WAREHOUSE_ID",
        //   component: () => import("@/components/wms/commonInput"),
        //   type: "inputText",
        //   isMust: false,
        //   readonly: true,
        //   showPlaceholder: "",
        //   topComponents: true
        // },
        {
          compKey: "edit_compKey5",
          labelName: "是否预测",
          codeField: "IF_FORECAST",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          options: [
            {
              label: "是",
              value: "1"
            },
            {
              label: "否",
              value: "0"
            }
          ],
          topComponents: true
        },
         {
          compKey: "edit_compKey6",
          labelName: "是否直送分库",
          codeField: "IS_STRAIGHT",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          options: [
            {
              label: "是",
              value: "1"
            },
            {
              label: "否",
              value: "0"
            }
          ],
          topComponents: true
        },
     
        {
          compKey: "edit2_compKey1",
          labelName: "安全库存调整系数(A)值",
          codeField: "SAFETY_FACTOR",
          component: () => import("@/components/wms/commonInput/indexSlot"),
          type: "inputText",
          isMust: true,
          isInput: true,
          changed: val => {
            this.$refs["edit2_compKey1"][0].modelCode = (
              this.formField.SAFETY_FACTOR + ""
            )
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
            this.formField.SAFETY_FACTOR = (this.formField.SAFETY_FACTOR + "")
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
          },
          dataToObject: {
            maxLength: 15,
            maxWordCount: 15
          },
          labelWidth: 130,
          bottomComponents: true,
          isSlot: true
        },
        {
          compKey: "edit2_compKey2",
          labelName: "订单周期（OP）",
          codeField: "ORDER_PERIOD",
          component: () => import("@/components/wms/commonInput/indexSlot"),
          type: "inputText",
          isMust: true,
          isInput: true,
          changed: val => {
            this.$refs["edit2_compKey2"][0].modelCode = (
              this.formField.ORDER_PERIOD + ""
            )
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
            this.formField.ORDER_PERIOD = (this.formField.ORDER_PERIOD + "")
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
          },
          dataToObject: {
            maxLength: 15,
            maxWordCount: 8
          },
          labelWidth: 100,
          bottomComponents: true,
          isSlot: true
        },
        {
          compKey: "edit2_compKey3",
          labelName: "到货周期（AP）",
          codeField: "ARRIVAL_PERIOD",
          component: () => import("@/components/wms/commonInput/indexSlot"),
          type: "inputText",
          isMust: true,
          isInput: true,
          changed: val => {
            this.$refs["edit2_compKey3"][0].modelCode = (
              this.formField.ARRIVAL_PERIOD + ""
            )
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
            this.formField.ARRIVAL_PERIOD = (this.formField.ARRIVAL_PERIOD + "")
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
          },
          dataToObject: {
            maxLength: 15,
            maxWordCount: 8
          },
          labelWidth: 100,
          bottomComponents: true,
          isSlot: true
        },
        {
          compKey: "edit2_compKey4",
          labelName: "统计月基数（MQ）",
          codeField: "MONTH_QTY",
          component: () =>
            import("@/components/wms/dropdownList/dropdownListSlot"),
          type: "dropdownList",
          isMust: true,
          options: [
            {
              label: "3",
              value: "3"
            },
            {
              label: "6",
              value: "6"
            },
            {
              label: "9",
              value: "9"
            }
          ],
          dataToObject: {
            maxWordCount: 10
          },
          labelWidth: 110,
          bottomComponents: true,
          isSlot: true,
          disabled: false
        },
        {
          compKey: "edit2_compKey5",
          labelName: "月销售波动偏差（SFD）",
          codeField: "SALE_FLUCTATE_DIFF",
          component: () => import("@/components/wms/commonInput/indexSlot"),
          type: "inputText",
          isMust: true,
          isInput: true,
          changed: val => {
            this.$refs["edit2_compKey5"][0].modelCode = (
              this.formField.SALE_FLUCTATE_DIFF + ""
            )
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
            this.formField.SALE_FLUCTATE_DIFF = (
              this.formField.SALE_FLUCTATE_DIFF + ""
            )
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
          },
          dataToObject: {
            maxLength: 15,
            maxWordCount: 12
          },
          labelWidth: 140,
          bottomComponents: true,
          isSlot: true,
          disabled: false
        },
        {
          compKey: "edit2_compKey6",
          labelName: "目标服务率（TSR）",
          codeField: "TARGET_SERVICE_RATE",
          textField: "SERVICE_RATE_FACTOR",    //TARGET_SERVICE_RATE 100%  //SERVICE_RATE_FACTOR 0.9
          oFields: "value,label",
          component: () =>
            import("@/components/wms/dropdownList/dropdownListSlot"),
          type: "dropdownList",
          isMust: true,
          options: [],
          bottomComponents: true,
          isSlot: true,
          dataToObject: {
            maxWordCount: 10
          },
          labelWidth: 120,
          slotWidth: 130,
          changed: val => {
            this.formField.TARGET_SERVICE_RATE = this.formField.TARGET_SERVICE_RATE.replace('%','')

          },
        },
        {
          compKey: "edit2_compKey7",
          labelName: "安全库存（SS）",
          codeField: "SAFETY_STORE",
          component: () => import("@/components/wms/commonInput/indexSlot"),
          type: "inputText",
          isMust: true,
          isInput: true,
          changed: val => {
            this.$refs["edit2_compKey7"][0].modelCode = (
              this.formField.SAFETY_STORE + ""
            )
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
            this.formField.SAFETY_STORE = (this.formField.SAFETY_STORE + "")
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
          },
          dataToObject: {
            maxLength: 15,
            maxWordCount: 12
          },
          labelWidth: 110,
          bottomComponents: true,
          isSlot: true
        },
        {
          compKey: "edit2_compKey8",
          labelName: "月均需求数量（AMC）",
          codeField: "AMC",
          component: () => import("@/components/wms/commonInput/indexSlot"),
          type: "inputText",
          isMust: true,
          isInput: true,
          changed: val => {
            this.$refs["edit2_compKey8"][0].modelCode = (
              this.formField.AMC + ""
            )
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
            this.formField.AMC = (this.formField.AMC + "")
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
          },
          dataToObject: {
            maxLength: 15,
            maxWordCount: 12
          },
          labelWidth: 140,
          bottomComponents: true,
          isSlot: true,
          disabled: false
        },
        {
          compKey: "edit2_compKey9",
          labelName: "月到货波动偏差（AFD）",
          codeField: "ARRIVE_FLUCTATE_DIFF",
          component: () => import("@/components/wms/commonInput/indexSlot"),
          type: "inputText",
          isMust: true,
          isInput: true,
          changed: val => {
            this.$refs["edit2_compKey9"][0].modelCode = (
              this.formField.ARRIVE_FLUCTATE_DIFF + ""
            )
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
            this.formField.ARRIVE_FLUCTATE_DIFF = (
              this.formField.ARRIVE_FLUCTATE_DIFF + ""
            )
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
          },
          dataToObject: {
            maxLength: 15,
            maxWordCount: 12
          },
          labelWidth: 140,
          bottomComponents: true,
          isSlot: true,
          disabled: false
        },
        {
          compKey: "edit2_compKey10",
          labelName: "备注",
          span: 24,
          codeField: "REMARK",
          component: () => import("@/components/wms/textareaInput"),
          isMust: true,
          bottomComponents: true
        }
      ],
      // 表单数据（无需赋值，由混入对象赋值）
      formField: {
        PART_ID: "",
        PART_NO: "",
        PART_NAME: "",
        SUPPLIER_CODE: "",
        IF_FORECAST: "",
        IS_STRAIGHT:"",
        SAFETY_FACTOR: "",
        ORDER_PERIOD: "",
        ARRIVAL_PERIOD: "",
        MONTH_QTY: "",
        SALE_FLUCTATE_DIFF: "",
        TARGET_SERVICE_RATE: "",
        SERVICE_RATE_FACTOR: "",
        SAFETY_STORE: "",
        AMC: "",
        WAREHOUSE_NAME: "",
         WAREHOUSE_ID: "",
        ARRIVE_FLUCTATE_DIFF: "",
        AUTO_MONTH_QTY: "0",
        AUTO_SFD: "0",
        AUTO_SS: "0",
        AUTO_AMC: "0",
        AUTO_AFD: "0",
        REMARK: ""
      },
      // 控制明细弹窗
      isShow: false
    };
  },
  methods: {
    onCreated() {
      this.formField.TARGET_SERVICE_RATE  =  this.formField.TARGET_SERVICE_RATE * 100+"%";
      if (this.curPopupsVisible) {
        // alert(this.formField.TARGET_SERVICE_RATE)
        // this.loadComponents();
        request({
          url: "/mp/wm/vl/warehouse/ppPartList/queryTargetServiceRateType.do",
          method: "post",
          data: {}
        }).then(res => {
          if (res.listRow && res.listRow.length > 0) {
            res.listRow.map(item => {
              this.tableComponents[11].options.push({
                label: item.TARGET_SERVICE_RATE + "%",
                value: item.SERVICE_RATE_FACTOR
              });
            });
              this.formField.TARGET_SERVICE_RATE =this.formField.TARGET_SERVICE_RATE
        }
          
          
        });
      }
    },
    loadComponents() {
      // if (this.formField.AUTO_MONTH_QTY === "1") {
      //   this.tableComponents[8].disabled = true;
      // }
      // if (this.formField.AUTO_SFD === "1") {
      //   this.tableComponents[10].disabled = true;
      // }
      // if (this.formField.AUTO_SFD === "1") {
      //   this.tableComponents[12].disabled = true;
      // }
      // if (this.formField.AUTO_AMC === "1") {
      //   this.tableComponents[13].disabled = true;
      // }
      // if (this.formField.AUTO_AFD === "1") {
      //   this.tableComponents[14].disabled = true;
      // }
    },
    changeCheckBox(val, str) {
      // if (str === "AUTO_MONTH_QTY") {
      //   if (val === "1") {
      //     this.tableComponents[8].disabled = true;
      //   } else {
      //     this.tableComponents[8].disabled = false;
      //   }
      // }
      // if (str === "AUTO_SFD") {
      //   if (val === "1") {
      //     this.tableComponents[9].disabled = true;
      //   } else {
      //     this.tableComponents[9].disabled = false;
      //   }
      // }
      // if (str === "AUTO_SS") {
      //   if (val === "1") {
      //     this.tableComponents[11].disabled = true;
      //   } else {
      //     this.tableComponents[11].disabled = false;
      //   }
      // }
      // if (str === "AUTO_AMC") {
      //   if (val === "1") {
      //     this.tableComponents[13].disabled = true;
      //   } else {
      //     this.tableComponents[13].disabled = false;
      //   }
      // }
      // if (str === "AUTO_AFD") {
      //   if (val === "1") {
      //     this.tableComponents[14].disabled = true;
      //   } else {
      //     this.tableComponents[14].disabled = false;
      //   }
      // }
    },
    detailClose() {
      this.isShow = false;
    },
    //保存
    save() {
      this.$utils.validataMoth(this, "validpopup");
      // if (this.formField.AUTO_MONTH_QTY === "0" && !this.formField.MONTH_QTY) {
      //   this.$message({
      //     message: "请选择统计月基数（MQ）或设置为自动参数"
      //   });
      //   return;
      // }
      // if (
      //   this.formField.AUTO_SFD === "0" &&
      //   !this.formField.SALE_FLUCTATE_DIFF
      // ) {
      //   this.$message({
      //     message: "请输入月销售波动偏差（SFD）或设置为自动参数"
      //   });
      //   return;
      // }
      // if (this.formField.AUTO_SS === "0" && !this.formField.SAFETY_STORE) {
      //   this.$message({
      //     message: "请输入安全库存（SS）或设置为自动参数"
      //   });
      //   return;
      // }
      // if (this.formField.AUTO_AMC === "0" && !this.formField.AMC) {
      //   this.$message({
      //     message: "请输入月均需求数量（AMC）或设置为自动参数"
      //   });
      //   return;
      // }
      // if (
      //   this.formField.AUTO_AFD === "0" &&
      //   !this.formField.ARRIVE_FLUCTATE_DIFF
      // ) {
      //   this.$message({
      //     message: "请输入月到货波动偏差（AFD）或设置为自动参数"
      //   });
        // return;
      // }
      // this.formField.TARGET_SERVICE_RATE = this.formField.SERVICE_RATE_FACTOR
      // this.formField.TARGET_SERVICE_RATE = this.formField.SERVICE_RATE_FACTOR        
      if (this.valid) {
        var saveObj = JSON.parse(JSON.stringify(this.formField));
        //保存表单
        this.saveForm(saveObj);
        storage.remove(this.$route.query.pageCode);
      }
    }
  }
};
</script>
<style type="text/css" lang="scss" scoped>
.slotDom {
  width: 80px;
  display: inline-block;
  text-align: left;
  padding-left: 6px;
  .safety_factorSpan {
    display: inline-block;
    font-family: math;
    font-size: 13px;
  }
}
</style>